package com.tanhua.dubbo.mongo.api;


import cn.hutool.core.collection.CollUtil;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.mongo.UserLike;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboReference;

import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;


import java.util.ArrayList;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {






    @Autowired
    private MongoTemplate mongoTemplate;


    //互相喜欢数
    public long likeEachOther(Long userId) {
        //查询我喜欢的列表
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //获取我喜欢的用户id
        List<Long> likeUserIds = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            Long likeUserId = userLike.getLikeUserId();
            likeUserIds.add(likeUserId);
        }
        Query query1 = Query.query(Criteria.where("userId").in(likeUserIds).and("likeUserId").is(userId));
        return mongoTemplate.count(query1, UserLike.class);
    }

    //我喜欢的
    public long iLike(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    //粉丝
    public long likeMe(Long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    // 关注我
    public List<Long> fanCount(Integer page, Integer pagesize, Long userId) {
        // 关注我
        Criteria criteria1 = Criteria.where("likeUserId").in(userId);
        List<UserLike> userLikes1 = mongoTemplate.find(Query.query(criteria1).limit((page - 1) * pagesize).limit(pagesize), UserLike.class);

        return CollUtil.getFieldValues(userLikes1, "userId", Long.class);
    }

    @Override
    public List<Long> loveCount(Integer page, Integer pagesize, Long userId) {
        // 关注我
        Criteria criteria1 = Criteria.where("userId").is(userId);
        List<UserLike> userLikes1 = mongoTemplate.find(Query.query(criteria1).limit((page - 1) * pagesize).limit(pagesize), UserLike.class);

        return CollUtil.getFieldValues(userLikes1, "likeUserId", Long.class);
    }

    @Override
    public List<Long> eachLoveCount(Integer page, Integer pagesize, Long userId) {
        //互相的
        List<UserLike> userLikes2 = mongoTemplate.find(new Query(), UserLike.class);
        List<Long> likeUserId1 = CollUtil.getFieldValues(userLikes2, "likeUserId", Long.class);

        List<Long> ids = new ArrayList<>();//互相喜欢
        for (Long aLong : likeUserId1) {
            if (aLong == userId) {
                Long userId1 = mongoTemplate.findOne(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class).getUserId();
                ids.add(userId1);
            }
        }
        return ids;
    }

    //粉丝喜欢
    public String saveLike(Long userId, Long userLikeId) {
        //判断喜欢的关系是否存在
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(userLikeId));
        if (mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setUserId(userId);
        userLike.setLikeUserId(userLikeId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
        return userLike.getId().toHexString();
    }

   //取消喜欢
    public Boolean deleteLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        DeleteResult result = mongoTemplate.remove(query, UserLike.class);
        return result.getDeletedCount()==1;
    }



    //是否互相喜欢
    @Override
    public boolean isMutualLike(Long userId, Long likeUserId) {
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        Query query = Query.query(criteria);
        boolean b = mongoTemplate.exists(query, UserLike.class);
        return b;
    }

    //根据id查询
    @Override
    public UserLike findById(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Query query = Query.query(criteria);
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        return userLike;
    }

    //保存
    @Override
    public void save(UserLike userLike, Boolean isLike) {
        Criteria criteria = Criteria.where("userId").is(userLike.getUserId())
                .and("likeUserId").is(userLike.getLikeUserId())
                .and("isLike").is(isLike);
        boolean exists = mongoTemplate.exists(Query.query(criteria), UserLike.class);
        if (!exists){
            mongoTemplate.save(userLike);
        }
    }

    //修改
    @Override
    public void update(UserLike userLike, Boolean isLike) {
        Criteria criteria = Criteria.where("userId").is(userLike.getUserId())
                .and("likeUserId").is(userLike.getLikeUserId());
        Query query = Query.query(criteria);

        Update update=new Update();
        update.set("isLike",isLike);

        mongoTemplate.updateFirst(query,update, UserLike.class);
    }

    //是否喜欢
    @Override
    public Boolean findExists(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("likeUserId").is(likeUserId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, UserLike.class);

    }

    //根据用户id查询
    @Override
    public List<UserLike> findByUserId(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        return mongoTemplate.find(query,UserLike.class);

    }
}
